library(data.table)
library(Hmisc)
library(stringr)
library(cjoint)
library(multiwayvcov)
library(car)
library(pBrackets)
library(survey)

# set your working directory to appropriate location
setwd("...")

# read in anonymized data where each row is a respondent-task observation
amce_data <- fread("LDAsurveydata.csv", header = TRUE, stringsAsFactors = FALSE)

# set baseline factor values
amce_data$stage <- factor(amce_data$stage, levels=c("committee", "floor"))
amce_data$pty <- factor(amce_data$pty, levels=c("Republican", "Democrat"))
amce_data$num_const <- factor(amce_data$num_const, levels=c("Very low", "Low", "Moderate", "High"))
amce_data$seniority <- factor(amce_data$seniority, levels=c("Freshman", "1 term", "3 terms", "7 terms"))
amce_data$position <- factor(amce_data$position, levels=c("Oppose", "Undeclared", "Support"))
amce_data$cosponsor <- factor(amce_data$cosponsor, levels=c("No", "Yes"))
amce_data$cmte <- factor(amce_data$cmte, levels=c("No", "Yes"))
amce_data$cmteldr <- factor(amce_data$cmteldr, levels=c("None", "Subcommittee ranking member", "Subcommittee chair"))
amce_data$mov <- factor(amce_data$mov, levels=c("Less than 10%", "10% to 20%", "20% to 30%", "More than 30%"))
amce_data$PAC <- factor(amce_data$PAC, levels=c("No", "Yes; $100", "Yes; $500", "Yes; $1000"))
amce_data$issue <- factor(amce_data$issue, levels=c("ACR", "SCA"))

# read in conjoint design generated by Hainmueller et al application
generic_design <- makeDesign(type="file",
                             filename="design.dat")

# remove respondent-task rows where respondents did not complete binary choice task; 
# amce function cannot handle missingness
amce_data_bin <- amce_data[!which(is.na(amce_data$choice)),]

# use amce function to estimate both AMCEs when pooling across stages and ACIEs for floor and
# committee stages
results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr + stage +
                  stage:pty + stage:num_const + stage:seniority + stage:position + 
                  stage:cmte + stage:mov + stage:PAC +
                  stage:cosponsor + stage:cmteldr,
                  data = amce_data_bin, design = generic_design,
                respondent.id = "respondent", respondent.varying = "stage")
summary(results)

# to make plots/tables, I need the point estimates and confidence intervals for the AMCEs and 
# ACIEs that are calculated internally in the plot.amce function and not saved for output; in 
# order to obtain the point estimates and confidence intervals, I extract and manipulate the
# code for the cjoint package so that the function plot.amce.int will not yield the default
# plot, but will instead only calculate and return the point estimates and confidence intervals
source("AMCE_Funcs.R")

# obtaining point estimates and confidence intervals for Figure 2; Bonferroni correction for
# 20 hypothesis tests
d <- plot.amce.int(results, ci=0.9975, facet.names = "stage")

# Figure 2
pdf(file = "binchoice_pooled_40hyp.pdf", family = "Times", height = 15, width=8)
par(mar=c(5.1,17,2.75,.5))
plot(x=rev(c(d$pe[which(d$var=="positionUndeclared" & d$facet=="Unconditional")],
             d$pe[which(d$var=="positionSupport" & d$facet=="Unconditional")],
             d$pe[which(d$var=="cosponsorYes" & d$facet=="Unconditional")],
             d$pe[which(d$var=="ptyDemocrat" & d$facet=="Unconditional")],
             d$pe[which(d$var=="cmteYes" & d$facet=="Unconditional")],
             d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Unconditional")],
             d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Unconditional")],
             d$pe[which(d$var=="seniority1term" & d$facet=="Unconditional")],
             d$pe[which(d$var=="seniority3terms" & d$facet=="Unconditional")],
             d$pe[which(d$var=="seniority7terms" & d$facet=="Unconditional")],
             d$pe[which(d$var=="mov10to20" & d$facet=="Unconditional")],
             d$pe[which(d$var=="mov20to30" & d$facet=="Unconditional")],
             d$pe[which(d$var=="movMorethan30" & d$facet=="Unconditional")],
             d$pe[which(d$var=="numconstLow" & d$facet=="Unconditional")],
             d$pe[which(d$var=="numconstModerate" & d$facet=="Unconditional")],
             d$pe[which(d$var=="numconstHigh" & d$facet=="Unconditional")],
             d$pe[which(d$var=="PACYes100" & d$facet=="Unconditional")],
             d$pe[which(d$var=="PACYes500" & d$facet=="Unconditional")],
             d$pe[which(d$var=="PACYes1000" & d$facet=="Unconditional")])), 
      y=c(1:3, 6:8, 11:13, 16:18, 21:22, 25, 28, 31, 34:35),
      xlim=c(-.45,.45),
      xaxt="n",
      ylim=c(0.5, 37.5),
      pch=19,
      tck=-.02,
      cex.axis=0.9,
      cex=1.25,
      ylab="",
      yaxt="n",
      xlab="",
      axes = FALSE,
      panel.first = c(abline(v=0,lwd=2, col="gray90",lty=2)))
points(x=rep(0,9), y=c(4, 9, 14, 19, 23, 26, 29, 32, 36), pch=19, cex=1.25)
segments(x0=rev(c(d$lower[which(d$var=="positionUndeclared" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="positionSupport" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="cosponsorYes" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="ptyDemocrat" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="cmteYes" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="seniority1term" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="seniority3terms" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="seniority7terms" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="mov10to20" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="mov20to30" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="movMorethan30" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="numconstLow" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="numconstModerate" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="numconstHigh" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="PACYes100" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="PACYes500" & d$facet=="Unconditional")],
                  d$lower[which(d$var=="PACYes1000" & d$facet=="Unconditional")])),
        x1=rev(c(d$upper[which(d$var=="positionUndeclared" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="positionSupport" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="cosponsorYes" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="ptyDemocrat" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="cmteYes" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="seniority1term" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="seniority3terms" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="seniority7terms" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="mov10to20" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="mov20to30" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="movMorethan30" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="numconstLow" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="numconstModerate" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="numconstHigh" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="PACYes100" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="PACYes500" & d$facet=="Unconditional")],
                 d$upper[which(d$var=="PACYes1000" & d$facet=="Unconditional")])),
        y0=c(1:3, 6:8, 11:13, 16:18, 21:22, 25, 28, 31, 34:35))
        axis(1,at=c(-.4, -.3, -.2, -.1, 0, .1, .2, .3, .4), 
             labels = c("-40%", "-30%", "-20%", "-10%", "0%", "10%",
                        "20%", "30%", "40%"), cex.axis = 1.5)
        par(mgp=c(0,11,0))
        axis(2,at=c(1:4, 6:9, 11:14, 16:19, 21:23, 25:26, 28:29, 31:32, 34:36),
             las=2,
             labels=rev(c("Opposed (baseline)", "Undeclared", "Support",
                          "No (baseline)", "Yes",
                          "Republican (baseline)", "Democrat",
                          "No (baseline)", "Yes",
                          "No (baseline)", "Subcom. chair", "Subcom. ranking member",
                          "Freshman (baseline)", "1 term", "3 terms", "7 terms",
                          "Less than 10% (baseline)", "10% to 20%", "20% to 30%", "More than 30%",
                          "Very low (baseline)", "Low", "Moderate", "High",
                          "None (baseline)", "$100", "$500", "$1000")),
             tck=0,
             lwd = 0,
             line = 0,
             cex.axis=1.5, hadj=0)
par(mgp=c(0,13,0))
axis(2,at=c(5, 10, 15, 20, 24, 27, 30, 33, 37),
     las=2,
     labels=rev(c(expression(~bold(~underline("Position"))), 
                  expression(~bold(~underline("Cosponsor"))), 
                  expression(~bold(~underline("Party"))), 
                  expression(~bold(~underline("Committee member"))),
                  expression(~bold(~underline("Committee leadership"))), 
                  expression(~bold(~underline("Seniority"))), 
                  expression(~bold(~underline("Margin of victory"))), 
                  expression(~bold(~underline("Constituency strength"))),
                  expression(~bold(~underline("Campaign contributions"))))),
     tck=0,
     lwd = 0,
     line = 0,
     cex.axis=1.5, hadj=0)
mtext("Effect on selection", side = 1, at=0, line = 3, cex = 1.75)
text(rev(c(d$pe[which(d$var=="positionUndeclared" & d$facet=="Unconditional")],
           d$pe[which(d$var=="positionSupport" & d$facet=="Unconditional")],
           d$pe[which(d$var=="cosponsorYes" & d$facet=="Unconditional")],
           d$pe[which(d$var=="ptyDemocrat" & d$facet=="Unconditional")],
           d$pe[which(d$var=="cmteYes" & d$facet=="Unconditional")],
           d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Unconditional")],
           d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Unconditional")],
           d$pe[which(d$var=="seniority1term" & d$facet=="Unconditional")],
           d$pe[which(d$var=="seniority3terms" & d$facet=="Unconditional")],
           d$pe[which(d$var=="seniority7terms" & d$facet=="Unconditional")],
           d$pe[which(d$var=="mov10to20" & d$facet=="Unconditional")],
           d$pe[which(d$var=="mov20to30" & d$facet=="Unconditional")],
           d$pe[which(d$var=="movMorethan30" & d$facet=="Unconditional")],
           d$pe[which(d$var=="numconstLow" & d$facet=="Unconditional")],
           d$pe[which(d$var=="numconstModerate" & d$facet=="Unconditional")],
           d$pe[which(d$var=="numconstHigh" & d$facet=="Unconditional")],
           d$pe[which(d$var=="PACYes100" & d$facet=="Unconditional")],
           d$pe[which(d$var=="PACYes500" & d$facet=="Unconditional")],
           d$pe[which(d$var=="PACYes1000" & d$facet=="Unconditional")])), 
        rep(c(1:3, 6:8, 11:13, 16:18, 21:22, 25, 28, 31, 34:35),2) + 0.5,
        paste0(sprintf("%.0f",round(rev(c(d$pe[which(d$var=="positionUndeclared" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="positionSupport" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="cosponsorYes" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="ptyDemocrat" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="cmteYes" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="seniority1term" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="seniority3terms" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="seniority7terms" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="mov10to20" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="mov20to30" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="movMorethan30" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="numconstLow" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="numconstModerate" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="numconstHigh" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="PACYes100" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="PACYes500" & d$facet=="Unconditional")],
                                          d$pe[which(d$var=="PACYes1000" & d$facet=="Unconditional")]
        ))*100, 0)),"%"), cex=1.5)
brackets(x1=-1.05, x2=-1.05, y1=0.60, y2=15.40, type=4, ticks = NA, xpd=TRUE, h=0.04)
mtext(expression(~bold("Electoral Concerns")), side = 2, at=8, line = 15, cex = 1.75)
brackets(x1=-1.05, x2=-1.05, y1=15.60, y2=30.40, type=4, ticks = NA, xpd=TRUE, h=0.04)
mtext(expression(~bold("Institutional Roles")), side = 2, at=22.75, line = 15, cex = 1.75)
brackets(x1=-1.05, x2=-1.05, y1=30.60, y2=37.40, type=4, ticks = NA, xpd=TRUE, h=0.04)
mtext(expression(~bold("Expressed Preferences")), side = 2, at=33.75, line = 15, cex = 1.75)
dev.off()

# obtaining point estimates and confidence intervals for Figure 3; Bonferroni correction for
# 40 hypothesis tests
d <- plot.amce.int(results, ci=0.99875, facet.names = "stage")

# Figure 3
pdf(file = "binchoice_cond_stage_40hyp.pdf", family = "Times", height = 15, width=13)
par(mar=c(5.1,17,2.75,.5))
plot(x=rev(c(d$pe[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = committee")],
             d$pe[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = floor")]+1,
             d$pe[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = floor")]+1)), 
      y=rep(c(1:3, 6:8, 11:13, 16:18, 21:22, 25, 28, 31, 34:35),2),
      xlim=c(-.45,1.45),
      xaxt="n",
      ylim=c(0.5, 37.5),
      pch=19,
      tck=-.02,
      cex.axis=0.9,
      cex=1.25,
      ylab="",
      yaxt="n",
      xlab="",
      axes = FALSE,
      panel.first = c(abline(v=0,lwd=2, col="gray90",lty=2),
                      abline(v=1,lwd=2, col="gray90",lty=2)))
points(x=c(rep(0,9), rep(1,9)), y=rep(c(4, 9, 14, 19, 23, 26, 29, 32, 36),2), pch=19, cex=1.25)
segments(x0=rev(c(d$lower[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = committee")],
                  d$lower[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = floor")]+1,
                  d$lower[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = floor")]+1)),
        x1=rev(c(d$upper[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = committee")],
                 d$upper[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = floor")]+1,
                 d$upper[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = floor")]+1)),
        y0=rep(c(1:3, 6:8, 11:13, 16:18, 21:22, 25, 28, 31, 34:35),2))
        axis(1,at=c(-.4, -.3, -.2, -.1, 0, .1, .2, .3, .4), 
             labels = c("-40%", "-30%", "-20%", "-10%", "0%", "10%",
                        "20%", "30%", "40%"), cex.axis = 1.5)
        axis(1,at=c(.6, .7, .8, .9, 1, 1.1, 1.2, 1.3, 1.4), 
             labels = c("-40%", "-30%", "-20%", "-10%", "0%", "10%",
                        "20%", "30%", "40%"), cex.axis = 1.5)
        par(mgp=c(0,11,0))
        axis(2,at=c(1:4, 6:9, 11:14, 16:19, 21:23, 25:26, 28:29, 31:32, 34:36),
             las=2,
             labels=rev(c("Opposed (baseline)", "Undeclared", "Support",
                          "No (baseline)", "Yes",
                          "Republican (baseline)", "Democrat",
                          "No (baseline)", "Yes",
                          "No (baseline)", "Subcom. chair", "Subcom. ranking member",
                          "Freshman (baseline)", "1 term", "3 terms", "7 terms",
                          "Less than 10% (baseline)", "10% to 20%", "20% to 30%", "More than 30%",
                          "Very low (baseline)", "Low", "Moderate", "High",
                          "None (baseline)", "$100", "$500", "$1000")),
             tck=0,
             lwd = 0,
             line = 0,
             cex.axis=1.5, hadj=0)
par(mgp=c(0,13,0))
axis(2,at=c(5, 10, 15, 20, 24, 27, 30, 33, 37),
     las=2,
     labels=rev(c(expression(~bold(~underline("Position"))), 
                  expression(~bold(~underline("Cosponsor"))), 
                  expression(~bold(~underline("Party"))), 
                  expression(~bold(~underline("Committee member"))),
                  expression(~bold(~underline("Committee leadership"))), 
                  expression(~bold(~underline("Seniority"))), 
                  expression(~bold(~underline("Margin of victory"))), 
                  expression(~bold(~underline("Constituency strength"))),
                  expression(~bold(~underline("Campaign contributions"))))),
     tck=0,
     lwd = 0,
     line = 0,
     cex.axis=1.5, hadj=0)
mtext("Effect on selection", side = 1, at=0, line = 3, cex = 1.75)
mtext("Effect on selection", side = 1, at=1, line = 3, cex = 1.75)
mtext("Committee stage", side = 3, at=0, line=0.5, cex = 2.5)
mtext("Floor stage", side = 3, at=1, line=0.5, cex = 2.5)
text(rev(c(d$pe[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = committee")],
           d$pe[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = floor")]+1,
           d$pe[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = floor")]+1)), 
    rep(c(1:3, 6:8, 11:13, 16:18, 21:22, 25, 28, 31, 34:35),2) + 0.5,
    paste0(sprintf("%.0f",round(rev(c(d$pe[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = committee")],
                                      d$pe[which(d$var=="positionUndeclared" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="positionSupport" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="cosponsorYes" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="ptyDemocrat" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="cmteYes" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="cmteldrSubcommitteechair" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="cmteldrSubcommitteerankingmember" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="seniority1term" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="seniority3terms" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="seniority7terms" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="mov10to20" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="mov20to30" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="movMorethan30" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="numconstLow" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="numconstModerate" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="numconstHigh" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="PACYes100" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="PACYes500" & d$facet=="Conditional on\nstage = floor")],
                                      d$pe[which(d$var=="PACYes1000" & d$facet=="Conditional on\nstage = floor")]))*100, 0)),"%"), cex=1.5)
brackets(x1=-1.10, x2=-1.10, y1=0.60, y2=15.40, type=4, ticks = NA, xpd=TRUE, h=0.04)
mtext(expression(~bold("Electoral Concerns")), side = 2, at=8, line = 15, cex = 1.75)
brackets(x1=-1.10, x2=-1.10, y1=15.60, y2=30.40, type=4, ticks = NA, xpd=TRUE, h=0.04)
mtext(expression(~bold("Institutional Roles")), side = 2, at=22.75, line = 15, cex = 1.75)
brackets(x1=-1.10, x2=-1.10, y1=30.60, y2=37.40, type=4, ticks = NA, xpd=TRUE, h=0.04)
mtext(expression(~bold("Expressed Preferences")), side = 2, at=33.75, line = 15, cex = 1.75)
dev.off()